﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace LoL_Mastery_Manager
{
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// Windows Message System Command 
        /// </summary>
        public const int WmSysCommand = 0x0112;

        /// <summary>
        /// System command to close the window.
        /// </summary>
        public const int ScClose = 0xF060;


        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        private static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool IsWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        //Custom main method
        [STAThread]
        public static void Main()
        {
            var search = FindWindowByCaption(IntPtr.Zero, "LoL Mastery Manager");
            if (IsWindow(search))
            {
                SendMessage(search, WmSysCommand, ScClose, 0);
                MessageBox.Show(
                    "Another instance of LoL Mastery Manager was running, and has been terminated!\r\nIf running the program from the wrong location, your builds may not load!",
                    "Notice!",
                    MessageBoxButton.OK,
                    MessageBoxImage.Information);
            }

            // Create new instance of application subclass
            var app = new App();

            // Code to register events and set properties that were
            // defined in XAML in the application definition
            app.InitializeComponent();

            // Start running the application
            app.Run();
        }
    }
}